package top.honlnk.common.config;

import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.honlnk.common.interceptor.JWTInterceptor;
import top.honlnk.common.interceptor.KeyInterceptor;
import top.honlnk.common.interceptor.PowerJWTInterceptor;
import top.honlnk.common.interceptor.SubmitIdInterceptor;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE");
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 指定controller统一的接口前缀
        configurer.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class));
    }

    @Resource
    private KeyInterceptor keyInterceptor;
    @Resource
    private SubmitIdInterceptor submitIdInterceptor;
    @Resource
    private JWTInterceptor jwtInterceptor;
    @Resource
    private PowerJWTInterceptor powerJWTInterceptor;

    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        registry.addInterceptor(keyInterceptor)
                .addPathPatterns(
                        "/api/user/upload",         // 用户上传课程表
                        "/api/admin/update_rota",   // 管理员修改值班表
                        "/api/admin/produce_rota",  // 管理员生成值班表
                        "/api/admin/get_user_table",// 获取用户课程表
                        "/api/admin/del_user_table",// 删除用户课程表
                        "/api/get/rota",            // 获取值班表
                        "/api/service_staff/get_table_by_key",  // staff获取课程表
                        "/api/service_staff/update_rota",       // staff修改值班表
                        "/api/service_staff/del_rota"           // staff删除值班表
                );

        registry.addInterceptor(submitIdInterceptor)
                .addPathPatterns(
                        "/api/user/update_table",       // 用户修改课程表
                        "/api/user/get_table",          // 用户获取课程表
                        "/api/admin/del_user_table",    // 删除用户课程表
                        "/api/service_staff/update_table",  // staff修改课程表
                        "/api/service_staff/del_table"      // staff删除课程表
                );

        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/service_staff/**")
                .excludePathPatterns("/api/service_staff/login");

        registry.addInterceptor(powerJWTInterceptor)
                .addPathPatterns("/api/service_staff/**")
                .excludePathPatterns(
                        "/api/service_staff/login**",
                        "/api/service_staff/get**",
                        "/api/service_staff/**ocr**",
                        "/api/service_staff/refresh_access_token"
                );
    }

}
